import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Router from 'vue-router'
import store from './store'
import axios from 'axios'
import echarts from 'echarts'
import md5 from 'js-md5';
import ViewUI from 'view-design';
import 'view-design/dist/styles/iview.css';
import { Loading,Icon,Table,TableColumn,Checkbox, Radio,Upload ,Image,Switch} from 'element-ui';
import './rem'
import { DropdownMenu,DropdownItem,Dialog  ,Popup ,PullRefresh , Picker ,Cell ,Field,List ,Swipe,SwipeItem} from 'vant';
import 'vant/lib/index.css';

import VideoPlayer from 'vue-video-player'
Vue.use(VideoPlayer)
require('vue-video-player/src/custom-theme.css')


//swiper
import VueAwesomeSwiper from 'vue-awesome-swiper'
// import style (>= Swiper 6.x)
import 'swiper/swiper-bundle.css'
Vue.use(VueAwesomeSwiper, /* { default options with global component } */)
//复制
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

Vue.use(Router)

Vue.use(Picker);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(Cell);
Vue.use(SwipeItem);
Vue.use(Swipe);
Vue.use(List);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Radio);
Vue.use(Checkbox);
Vue.use(Upload);
Vue.use(Image);
Vue.use(Switch);
Vue.use(PullRefresh);
Vue.use(Field);
Vue.use(Popup);
Vue.use(Dialog);


Vue.use(ViewUI);
Vue.use(Loading.directive)
Vue.use(Icon)

const originalPush = Router.prototype.push
   Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}
Vue.prototype.$md5 = md5;
Vue.prototype.$echarts = echarts
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  if(response.data.status==-2){
    localStorage.token = ''
    window.location.href = window.location.href.split('/')[0] + '/login'
  }
  return response;
})

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  config.headers.Authorization = localStorage.token||''
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

router.beforeEach((to, from , next) => {
  document.title = to.meta.title
  if(to.name!='login'&&!localStorage.token&&(to.name.indexOf('Detail')==-1)){
    window.location.href = window.location.href.split('/')[0] + '/login'
  }else{
    next();
  }
})


Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
